app.controller('goodsDetailEditCtrl',['$scope','$state','$stateParams','goodsDetailEditService','toaster',function($scope,$state,$stateParams,goodsDetailEditService,toaster){
    $scope.skus = new Array();
    $scope.skuContents = new Array();
    $scope.goodsDetailEditService=goodsDetailEditService;
    $scope.addgoods=function(){
        goodsDetailEditService.addgoods();
    }

    $scope.brands;
    $scope.types;

    //查询连锁信息
    $scope.queryShop=function(){
        goodsDetailEditService.queryShop($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.chain=result.data;
                    goodsDetailEditService.goods=initGoods;
                    break;
                default :
                    break;
            }
        })
    }
    $scope.queryShop();


    //查询品牌信息
    $scope.querybrands=function(){
        goodsDetailEditService.querybrands($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.brands=result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.querybrands();

    //查询类型信息
    $scope.queryType=function(){
        goodsDetailEditService.queryType($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.types=result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.queryType();


    //查询系统分类
    $scope.systemTypesArr=[];

    $scope.querySystemType = function () {
        goodsDetailEditService.querySystemType($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.systemTypes = result.data;
                    for(var i=0;i<$scope.systemTypes.length;i++){
                        var index=$scope.goodsDetailEditService.goods.systemTypeId.indexOf($scope.systemTypes[i].id);
                        if(index>-1){
                            $scope.systemTypesArr.push($scope.systemTypes[i]);
                        }
                    }
                    break;
                default:
                    break;
            }
        })
    }

    $scope.changeSystemType=function(obj){
        $scope.goodsDetailEditService.goods.systemTypeId=[];
        for(var i=0;i<obj.length;i++){
            $scope.goodsDetailEditService.goods.systemTypeId.push(obj[i].id);
        }
    }

    //查询运费模板
    $scope.queryPostageTemplate=function(){
        goodsDetailEditService.queryPostageTemplate($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.postageTemplate=result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.queryPostageTemplate();


    //查询商品信息
    $scope.id=$stateParams.id;
    $scope.chainGoodsId=$stateParams.id;
    $scope.findById=function(){
        goodsDetailEditService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.goodsDetailEditService.goods=result.data;
                    if($scope.goodsDetailEditService.goods.status==1){
                        $scope.allowNotEdit=true;
                    }

                    //$scope.querySystemType();

                    break;
                case 646:
                    toaster.pop('waiting', "提示", result.message);
                    $state.go('app.404');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        })
    }
debugger;
    $scope.findById();

    $scope.changeSellType=function(type){
        if(type==0){
            goodsDetailEditService.goods.isOpenSell=0;
            goodsDetailEditService.goods.isOpenRebate=0;
            goodsDetailEditService.goods.profit=0;
        }else if(type==1){
            goodsDetailEditService.goods.isOpenSell=1;
            goodsDetailEditService.goods.isOpenRebate=0;
            goodsDetailEditService.goods.profit=$scope.chain.profit;
        }else if(type==2){
            goodsDetailEditService.goods.isOpenSell=0;
            goodsDetailEditService.goods.isOpenRebate=1;
            if($scope.chain.profit<26){
                goodsDetailEditService.goods.profit=26;
            }else{
                goodsDetailEditService.goods.profit=$scope.chain.profit;
            }
        }else if(type==3){
            goodsDetailEditService.goods.isOpenSell=1;
            goodsDetailEditService.goods.isOpenRebate=1;
            if($scope.chain.profit<26){
                goodsDetailEditService.goods.profit=26;
            }else{
                goodsDetailEditService.goods.profit=$scope.chain.profit;
            }
        }
    };


    //修改商品
    var initGoods={
        isOpenSku: 0,
        isFreeShipping: 0,
        isOpenMember: 0,
        isRecommend: 0,
        isOpenRebate: 0,
        isHelpPeasant:0,
        postageType: 0,
        systemTypeId:[],
        techniciansId:[]
    };

    $scope.updateState=false;

    $scope.updateGoods=function(){

        if($scope.updateState){
            return;
        }

        $scope.updateState=true;

        //预约参数
        if(goodsDetailEditService.goods.type==4){
            goodsDetailEditService.goods.reserveTime=goodsDetailEditService.goods.startTime+'-'+goodsDetailEditService.goods.endTime;
            goodsDetailEditService.goods.canReserve=selectWeek();
        }else{
            delete goodsDetailEditService.goods.startTime;
            delete goodsDetailEditService.goods.endTime;
            delete goodsDetailEditService.goods.canReserve;
            goodsDetailEditService.goods.time='';
        }

        goodsDetailEditService.updateGoods($scope).then(function(result){
            switch (result.status){
                case 200:
                    if($scope.goodsDetailEditService.goods.isOpenSku==1){
                        $scope.saveGoodsSku(result.data.id);
                    }else{
                        toaster.pop('success', "提示", "操作成功");
                        $scope.updateState=false;
                        $state.go('app.goodsDetailPictures', {chainGoodsId: $scope.chainGoodsId});
                    }
                    break;
                default :
                    $scope.updateState=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    //修改周期
    var selectWeek=function(){
        var day='';
        for(var i=0;i<$scope.weeks.length;i++){
            if($scope.weeks[i].state){
                day+=$scope.weeks[i].name+',';
            }
        }
        var index=day.lastIndexOf(',');
        if(index>-1){
            day=day.substring(0,index);
        }
        return day;
    }

    //保存商品sku
    $scope.saveGoodsSku=function(goodsId){

        goodsDetailEditService.saveGoodsSku($scope).then(function(result){
            switch (result.status) {
                case 200:
                    $state.go('app.goodsDetailPictures', {chainGoodsId: $scope.chainGoodsId});
                    $scope.updateState=false;
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    $scope.updateState=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });

    }


    $scope.$on('skuContents', function(event, data) {
        $scope.skuContents=data;
    });

    $scope.$on('skus', function(event, data) {
        $scope.skus=data;
        $scope.backSkus=data;
    });

    //开始sku 初始化
    $scope.openSku=function(){
        if($scope.goodsDetailEditService.goods.isOpenSku==1){
            $scope.goodsDetailEditService.goods.price=0.00;
            $scope.goodsDetailEditService.goods.stock=0;
            $scope.skus=$scope.backSkus;
        }else if($scope.goodsDetailEditService.goods.isOpenSku==0){
            $scope.skus=[];
        }
    }





}])